package com.blackshadowgame.rps.slice;

import com.blackshadowgame.rps.*;
import com.blackshadowgame.rps.client.GameClient;
import com.blackshadowgame.utils.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.IntervalTimer;

public class GamePlaySlice extends BaseSlice implements Component.ClickedListener {
    private static final String TAG = GamePlaySlice.class.getSimpleName();
    public String getTag(){
        return TAG;
    }

    protected int getLayoutId(){
        return ResourceTable.Layout_ability_main_game_slice;
    }

    protected int getTextLogId(){
        return ResourceTable.Id_text_info;
    }

    private boolean gameFinished = false;

    private Text txtResult = null;
    private Text txtService = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        addInfo("准备出拳，你的id是: " + GameClient.getInstance().getPlayerId());
//        startGameLoop();
    }

    protected void initUIComponent() {
        super.initUIComponent();

        ((Button)findComponentById(ResourceTable.Id_buttonCodeRock)).setText(InputCode.getInputCodeName(InputCode.ROCK_1));
        ((Button)findComponentById(ResourceTable.Id_buttonCodeRock)).setClickedListener(this);

        ((Button)findComponentById(ResourceTable.Id_buttonCodeScissors)).setText(InputCode.getInputCodeName(InputCode.SCISSORS_2));
        ((Button)findComponentById(ResourceTable.Id_buttonCodeScissors)).setClickedListener(this);

        ((Button)findComponentById(ResourceTable.Id_buttonCodePaper)).setText(InputCode.getInputCodeName(InputCode.PAPER_3));
        ((Button)findComponentById(ResourceTable.Id_buttonCodePaper)).setClickedListener(this);

        ((Button)findComponentById(ResourceTable.Id_buttonStopGame)).setClickedListener(this);

        txtResult = (Text)findComponentById(ResourceTable.Id_text_result);
        txtService = (Text)findComponentById(ResourceTable.Id_text_service);
        txtResult.setText("开始游戏");
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        switch (id){
            case ResourceTable.Id_buttonCodeRock:
                sendCode(InputCode.ROCK_1);
                break;
            case ResourceTable.Id_buttonCodeScissors:
                sendCode(InputCode.SCISSORS_2);
                break;
            case ResourceTable.Id_buttonCodePaper:
                sendCode(InputCode.PAPER_3);
                break;
            case ResourceTable.Id_buttonStopGame:
                onStopGame();
                break;
        }
    }

    private void sendCode(int code){
        int errorCode = GameClient.getInstance().sendInput(code);
            if(errorCode != 0){
            addInfo("出拳失败，错误码：" + errorCode);
        }else {
            addInfo("出拳：" + InputCode.getInputCodeName(code));
            txtResult.setText("你的出拳：" + InputCode.getInputCodeName(code));
        }
    }

    private void onStopGame(){
        addInfo("退出游戏");

        GameClient.getInstance().leaveGame();
//        GameClient.getInstance().disconnect();
        goHomeSlice();
    }

    private void onWaitingPlayer(){
        addInfo("有玩家退出了！");

        goWaitSlice();
    }

    protected void onLoop(long deltaTime){
        super.onLoop(deltaTime);

        txtService.setText("游戏服务:" + GameClient.getInstance().getServiceName() + ", ID: " + GameClient.getInstance().getPlayerId());
        if(!GameClient.getInstance().isConnected()){
            addInfo("====> 连接断开！！！");
            goHomeSlice();
            return;
        }

        GameResultData result = GameClient.getInstance().queryGameResult();
        if(gameFinished){
            gameFinished = false;
            if(result.state != GameCode.GAME_STATE_FINISHED){
                addInfo("对方已经开始新游戏...");
            }else{
                addInfo("自动开始下一局");
                GameClient.getInstance().nextGame();
            }
        }else{
            if(result.state == GameCode.GAME_STATE_NEW){
                addInfo("新游戏开始...");
            }else if(result.state == GameCode.GAME_STATE_WAITING_FOR_PLAYER){
                addInfo("等待其他玩家进入游戏...");
                onWaitingPlayer();
            }else if(result.state == GameCode.GAME_STATE_WAITING_FOR_INPUT){
                GameInput input = result.getPlayerInput(GameClient.getInstance().getPlayerId());
                if(input == null || input.inputCode == InputCode.NONE_0){
                    addInfo("你还没有出拳");
                    txtResult.setText("轮到你出拳了");
                }else{
                    addInfo("等待玩家出拳...");
                    String info = String.format("你的出拳是: [%s], 等待对方出拳", InputCode.getInputCodeName(input.inputCode));
                    txtResult.setText(info);
                }

            }else if(result.state == GameCode.GAME_STATE_FINISHED){
                addInfo("游戏结束");

                String info = "";
                for(GameInput player: result.playerInputs){
                    info += String.format("玩家[%d]:[%s] ", player.playerId, InputCode.getInputCodeName(player.inputCode));
                }
                info += String.format("胜者：[%d]", result.winner);
                if(result.winner < 0){
                    info += "\n你们打平了。";
                }else if(result.winner == GameClient.getInstance().getPlayerId()){
                    info += "\n你赢了！";
                }else{
                    info += "\n你输了！";
                }
                addInfo(info);

                txtResult.setText(info);
                gameFinished = true;
            }
        }

    }
}
